<?php
	$ariane = array(
		array(
			'href' => "index.php",
			'name' => "Home"
		),
		array(
			'href' => "ranking.php",
			'name' => "Track records"
		)
	);
	require("include/includes.php");
	
	$player = NULL;
	$url=NULL;
	$records = NULL;
	$isOwnerProfil = false;
	
	$begin 		= (int)getOrCustom('begin', 0);
	if($begin<0) $begin=0;
	$nb 		= 30;
	$country	= getOrCustom('country', -1);
	$pid 		= getOrCustom('pid', -1);
	$tid 		= getOrCustom('tid', 1);
	$penta		= (getOrCustom('penta', 0) == 'on')? 1:0; //TODO gérer penta comme ça partout!
	$pentaSelected = ($penta==1)? 'on':'off';
	$stp		= getOrCustom('stp', 0);
	$ice		= getOrCustom('ice', 0);

	$track 		= new TrackName($tid);

	$type 		= getOrCustom('type', COURSE);
	$trackType 	= new TrackType($type);
	$typeName 	= $trackType->getName();
	
	$mode 		= getOrCustom('mode', CLASSIC);
	$trackMode 	= new TrackMode($mode);
	$modeName 	= $trackMode->getMode();
	
	$countrySelected = new Country($country);
	
	// Si $type, ... = n'importe quoi et que ça ne corresponde à rien
	if(!$track->isLoaded()){
		$track = new TrackName(1);
		$tid = $track->getId();
	}
	
	if(empty($typeName)){
		$trackType = new TrackType(COURSE);
		$type = $trackType->getName();
	}
	
	if(empty($modeName)){
		$trackMode = new TrackMode(CLASSIC);
		$modeName = $trackMode->getMode();
	}
	
	$modeSelected = $trackMode->getId();
	$typeSelected = $trackType->getId();
	
	if($modeName=="classic")
	{
		$ice = 0;
		$stp = 0;
	}
	
	//$url = "http://".$_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
	$records = Record::getAllWithFilter(Array(
		'country'=>$country, 
		'penta'=>$penta, 
		'stp'=>$stp, 
		'ice'=>$ice,
		'player'=>$pid, 
		'type'=>$type,
		'mode'=>$mode,
		'name'=>$tid, //track id 
		'begin'=>$begin, 
		'nb'=>$nb+1
	));
	$countRecords = Record::getAllWithFilter(Array(
		'country'=>$country, 
		'penta'=>$penta, 
		'stp'=>$stp, 
		'ice'=>$ice,
		'player'=>$pid, 
		'type'=>$type,
		'mode'=>$mode,
		'name'=>$tid, //track id 
		'count'=>true,
	));
	
	$types = TrackType::getAll();
	$modes = TrackMode::getAll();
	$tracks = TrackName::getAll();
	// Création du titre
	$pageTitle = 'Ranking: '.$typeName." - ".$modeName;
	/*if(isset($_GET["mode"]) && isset($_GET["type"]) && $_GET["mode"]!=0) {
		$pageTitle .= $typeName." - ".$modeName;
		$modeSelected = $_GET["mode"];
		$typeSelected = $_GET["type"];
	}
	else if(isset($_GET["mode"]) && !isset($_GET["type"])) {
		$pageTitle .= $modeName;
		$modeSelected = $_GET["mode"];
		$typeSelected = 0;
	}
	else if((!isset($_GET["mode"]) || $_GET["mode"] == 0) && isset($_GET["type"])) {
		$pageTitle .= $typeName;
		$modeSelected = 0;
		$typeSelected = $_GET["type"];
	}*/
	
	if(!isset($_SESSION["user"]))
		$userId = 0;
	else
		$userId = $_SESSION["user"]->getId();
	
	$template = $twig->loadTemplate('ranking.html');
	$template->display(array(
		'pageTitle' => $pageTitle,
		'player' => $player,
		'url' => $url,
		'records' => $records,
		'isOwnerProfil' => $isOwnerProfil,
		'userId' => $userId,
		'types' => $types,
		'modes' => $modes,
		'tracks' => $tracks,
		'modeSelected' => $modeSelected,
		'typeSelected' => $typeSelected,
		'trackSelected' => $track,
		'pentaSelected' => $pentaSelected,
		'countrySelected' => $countrySelected,
		'begin' => $begin,
		'nb'=>$nb,
		'pageNextUrl'=>Paginator::paginationUrl($nb),
		'pagePreviousUrl'=>Paginator::paginationUrl(-$nb),
		'MEDIA_PATH'=>MEDIA_PATH,
		'pageNextImg'=>Paginator::IMAGE_NEXT,
		'pagePreviousImg'=>Paginator::IMAGE_PREV,
		'nbTotalLines'=>$countRecords,
		'paginator'=>Paginator::paginatorHtml($countRecords, $nb)
	));
?>